package com.mty.springboot.controller;

import com.mty.springboot.model.Loss;
import com.mty.springboot.model.Obtain;
import com.mty.springboot.model.PageInfo;
import com.mty.springboot.model.User;
import com.mty.springboot.service.ObtainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class ObtainController {

    // 依赖注入
    @Autowired
    private ObtainService obtainService;


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findObtain")
    public String findObtain(Integer pageIndex, Integer pageSize,String category,String remark, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("category",category);
        mp.put("remark",remark);
        mp.put("nosh","11");
        PageInfo<Obtain> pageList = obtainService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "ObtainList";
    }


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findNoPassObtain")
    public String findNoPassObtain(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("status","01");
        PageInfo<Obtain> pageList = obtainService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "NotObtainList";
    }

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/MyfindObtain")
    public String MyfindObtain(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        User user = (User) session.getAttribute("ad");
        mp.put("uid",user.getId());
        mp.put("nosh","11");
        PageInfo<Obtain> pageList = obtainService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "MyObtainList";
    }


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/MyPushObtain")
    public String MyPushObtain(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        User user = (User) session.getAttribute("ad");
        mp.put("pid",user.getId());
        mp.put("nosh","11");
        PageInfo<Obtain> pageList = obtainService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "MyPushObtain";
    }

    /**
     * 已发布待审核
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findInitObtain")
    public String findInitObtain(Integer pageIndex, Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("status","11");
        PageInfo<Obtain> pageList = obtainService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "ValidateObtainList";
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/addObtain" ,method = RequestMethod.POST)
    @ResponseBody
    public String addObtain( @RequestBody Obtain obtain,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "202";
        }
        User user = (User) session.getAttribute("ad");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM:dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        obtain.setPtime(time);
        obtain.setStatus("11");
        obtain.setPid(user.getId());
        int d = obtainService.addObtain(obtain);
        return "ObtainList";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteObtain")
    @ResponseBody
    public String deleteObtain(String id) {
        int d = obtainService.deleteObtain(id);
        return "ObtainList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateObtain")
    public String updateObtain( Obtain obtain) {
        int d = obtainService.updateObtain(obtain);
        return "redirect:/findObtain";
    }

    /**
     * 更新状态
     */
    @RequestMapping( "/updateObtainStatus")
    @ResponseBody
    public String updateObtainStatus(String id, String status) {
        try{
            Obtain obtain = new Obtain();
            obtain.setId(id);
            if(status.equals("03")){
                obtain.setStatus("00");
                obtain.setUid("");
                obtain.setContent("");
            }else{
                obtain.setStatus(status);
            }
            int d = obtainService.updateObtain(obtain);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }


    /**
     * 申请认领
     */
    @RequestMapping( value = "/renling",method = RequestMethod.POST)
    @ResponseBody
    public String renling(@RequestBody  Obtain obtain, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "202";
        }
        User user = (User) session.getAttribute("ad");
        obtain.setUid(user.getId());
        obtain.setStatus("01");
        int d = obtainService.updateObtain(obtain);
        return "ok";
    }


    /*** 按照用户选择的物品类型查询*/
    @RequestMapping( "/findObtainById")
    public String findObtainById(String id,Model model,HttpServletRequest request) {
        Obtain obtain= obtainService.findObtainById(id);
        model.addAttribute("obtain",obtain);
        return "ObtainEdit";
    }

    /*** 按照用户输入的物品特征关键词查询*/
    @RequestMapping( value = "/queryObtainInfo",method = RequestMethod.POST)
    @ResponseBody
    public Obtain queryObtainInfo(String id, HttpServletRequest request) {
        Obtain obtain = obtainService.findObtainById(id);
        return  obtain;
    }
}
